package org.ethan.pet.service.impl;

import org.ethan.basic.service.impl.BaseServiceImpl;
import org.ethan.basic.util.DistanceUtil;
import org.ethan.basic.util.PageList;
import org.ethan.basic.util.Point;
import org.ethan.org.domain.Employee;
import org.ethan.org.domain.Shop;
import org.ethan.org.mapper.EmployeeMapper;
import org.ethan.org.mapper.ShopMapper;
import org.ethan.pet.domain.Pet;
import org.ethan.pet.domain.PetDetail;
import org.ethan.pet.domain.SearchMasterMsg;
import org.ethan.pet.mapper.PetDetailMapper;
import org.ethan.pet.mapper.PetMapper;
import org.ethan.pet.mapper.SearchMasterMsgMapper;
import org.ethan.pet.query.SearchMasterMsgQuery;
import org.ethan.pet.service.ISearchMasterMsgService;
import org.ethan.user.domain.User;
import org.ethan.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper detailMapper;



    /**
     * 发布领养信息
     * @param smm
     * @param loginInfo_id
     */
    @Override
    public void publish(SearchMasterMsg smm, Long loginInfo_id) {

        User user = userMapper.findByLoginInfoId(loginInfo_id);

        smm.setUser_id(user.getId());

        // 通过百度地图添加地址
        // 把当前用户输入的地址转化为经纬度
        Point point = DistanceUtil.getPoint(smm.getAddress());

        // 获取所有的店铺
        List<Shop> shops = shopMapper.selectAll();
        // 获取最近的店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        smm.setShop_id(nearestShop.getId());

        // 向数据库保存信息
        searchMasterMsgMapper.add(smm);

    }


    /**
     * 待处理的寻主信息
     * @param query
     * @param loginInfo_id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long loginInfo_id) {
        Employee employee = employeeMapper.findByLoginInfoId(loginInfo_id);
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        query.setState(0);

        return super.queryData(query);
    }


    /**
     * 已处理的寻主信息
     * @param query
     * @param loginInfo_id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long loginInfo_id) {
        Employee employee = employeeMapper.findByLoginInfoId(loginInfo_id);
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        query.setState(1);

        return super.queryData(query);
    }

    /**
     * 处理待处理的消息
     * @param pet
     * @param id
     */
    @Override
    @Transactional
    public void handle(Pet pet, Long id) {
        // 1.更新状态 state=1
        searchMasterMsgMapper.updateStateById(pet.getSearch_master_msg_id());

        // 2.保存t_pet
        petMapper.add(pet);

        // 3.保存到petdetail
        PetDetail detail = pet.getDetail();

        if (detail != null) {

            detail.setPet_id(pet.getId());
        }

        detailMapper.add(detail);

    }


//    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}
